/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.bean.questionnaire;

import org.ipti.br.tag.bean.AppBaseBean;
import org.ipti.br.tag.bean.SessionBean;
import org.ipti.br.tag.bo.*;
import org.ipti.br.tag.entity.Campaign;
import org.ipti.br.tag.entity.Unity;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.ipti.br.tag.bean.dataManipulation.WeightAndHeightAuxiliator;



/**
 *
 * @author FelipeMSX
 */
@Named @ViewScoped
public class WeightAndHeightBean extends AppBaseBean implements Serializable {
   
   @Inject private SessionBean sessionBean;
   @Inject private UnityBO unityBO;
   @Inject private CampaignBO campaignBO;
   @Inject private WeightAndHeightBO weightAndHeightBO;
  
   private Unity unity;
   private Campaign campaign;
   private List<WeightAndHeightAuxiliator> WHList;
   
   private int year = getActualYear();
 
   public WeightAndHeightBean(){
       
   }
   
   @PostConstruct
   public void init(){
       this.WHList = new ArrayList<>();
       this.campaign = null;
       this.unity = null;
   }
      
   public List<Unity> getUnityList() {
       if (this.campaign != null) {
           return unityBO.getClassLessonList(sessionBean.getLoggedActor().getUnity(), year);
       }else{
           return null;
       }
   }
   
   public List<Campaign> getCampaingList(){        
      return campaignBO.findAll();
   }

   public void loadTableData(){
       if (campaign != null && unity != null){
           WHList = weightAndHeightBO.getDataTableWaH(campaign, unity);
       }  
   } 
   
   public void save(){
       try{
           weightAndHeightBO.saveAll(WHList, campaign);
           addInfoMessage(":wahForm:message", "Sucesso!", "Os dados foram salvos com sucesso no servidor");
       }catch(Exception e){
           e.printStackTrace();
           addErrorMessage(":wahForm:message", "Erro ao salvar os dados no servidor", "Não foi possível salvar");
       }
       
   }
   
   public void print(){       
        boolean sucess = weightAndHeightBO.generatePdf(sessionBean, campaign, unity, WHList);
        if (sucess){
            addInfoMessage(":wahForm:message", "Sucesso!", "Os relatórios gerados estão lindos, né?");
        }else{
            addErrorMessage(":wahForm:message", "Ocorreu algum provlema ao gerar os relatórios", ":(");
        }
   }
   
   public boolean activeUnity(){
       return ( this.campaign == null );
   }
  
   public boolean activeBottoms(){
       return ( (this.WHList.isEmpty() ) || (this.campaign == null) || (this.unity == null)); 
   }
   
   public void rediretToHomepage(){
       redirect("/educational/home/home.xhtml");
   }

    public SessionBean getSessionBean() {
        return sessionBean;
    }

    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }

    public Campaign getCampaign() {
        return campaign;
    }

    public void setCampaign(Campaign campaign) {
        this.campaign = campaign;
    }

    public List<WeightAndHeightAuxiliator> getWHList() {
        return WHList;
    }

    public void setWHList(List<WeightAndHeightAuxiliator> WHList) {
        this.WHList = WHList;
    }

    public Unity getUnity() {
        return unity;
    }

    public void setUnity(Unity unity) {
        this.unity = unity;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
